% This scripts reads RAW ACE data, interpolate the gaps and calculate the
% eef reponse using the ppefm model and save the results to a file

load /data/backup/mnair/ace_tensor/acedata/ace_2000_2010.mat ace_all;
load /data/backup/mnair/longp/aplist.mat;
% Initializations
options.ap_lower_limit = 20; % FIXED at 20 lower limt of Ap
options.phase_delay = 17;% FIXED at 17 time delay for IEF to propagate from Bow-shock to ionopshere minutes
options.plc = 'c';% plot color
options.tol = 1.8/24; % max time interval tolerated
options.min_time_length = 34/24; % minimum time length required in decimal days
options.lt_start = 9; % FIXED 9-15 (data permits 7-17)
options.lt_end = 15;
options.overlap = 1;
options.champ_resampling = 1;
options.coh_plot_lim = 0.0;
options.ace_interp_method = 'spline'; %FIXED spline
options.minimum_period_plot = 6; % 5 if the min length is around 30-40 hours
options.maximum_period_plot = 40;
options.Eeff_cut_off = 8; % The cut off amplitude for IEF .LUHR & MAUS EPS 2010 used a value of 8
options.des_int = 2; %desired sampling interval in hours
% the sampling interval is set ~30 minutes above the
% champ EQ revisit time (92 minutes)
options.ace_gap_tol = ( options.min_time_length + 6/24 ) * 0.1 ;
options.ace_gap_len_tol = floor ( options.ace_gap_tol * 1440/5 );
% the gap tolerence is 10% of the ace data length
% finding gaps in ACE data with gaps > 12 minutes

nd = 1;
np = 0;

L = isnan(ace_all(:,2)) | isnan(ace_all(:,3));

for i = 1: length(ace_all) - 1,
    
    
    if  L(i) && L(i + 1)
        
        np = np + 1;
        
    else
        if ( ace_all(i,1) - ace_all(i - np ,1) > 12/1440 )
            % Mar 2012. One of the issues to be looked at is how these
            % gaps are affecting the spectra. A future improvement
            % should be to break the ACE data into pieces (just like
            % EEF data) and limit the Fourier analysis to within the
            % segements.
            data_index_ace(nd,2) = i;
            data_index_ace(nd,1) = i - np;
            nd = nd + 1;
            
        end;
        np = 0;
        
    end;
end;

L = isnan(ace_all(:,2));
y = interp1(ace_all(~L,1),ace_all(~L,2),ace_all(:,1),options.ace_interp_method);
ace_all(:,2) = y;

for i = 1:length(data_index_ace),
    %     ace_all(data_index_ace(i,1):data_index_ace(i,2),2) = 0;
    %     ace_all(data_index_ace(i,1):data_index_ace(i,2),3) = 0;
    %
    %linear interpolation for the gaps > 12 minutes. Note that for gaps
    %less than 12 minutes, we use the defined interpolation method (such as
    %spline)
    ace_all(data_index_ace(i,1):data_index_ace(i,2),2) = ...
        interp1(ace_all([data_index_ace(i,1)-1 data_index_ace(i,2)+1],1), ...
        ace_all([data_index_ace(i,1)-1 data_index_ace(i,2)+1],2), ...
        ace_all(data_index_ace(i,1):data_index_ace(i,2),1));
    ace_all(data_index_ace(i,1):data_index_ace(i,2),3) = ...
        interp1(ace_all([data_index_ace(i,1)-1 data_index_ace(i,2)+1],1), ...
        ace_all([data_index_ace(i,1)-1 data_index_ace(i,2)+1],3), ...
        ace_all(data_index_ace(i,1):data_index_ace(i,2),1));
end;

ace_predicted = ppefm(ace_all(:,2),'Ey');
eef_response = ace_predicted;
eef_fday = ace_all(:,1);
plot(eef_fday,eef_response,'r');

%save /home/mnair/projects/longp/predicted_eef eef_fday eef_response;